#coding=utf-8

from common.import_common import *
from conversation.forms import ConversationForm
from vote.models import Vote
from conversation.models import Conversation

@login_required   
def reply(request, vid, template_name="conversation/reply.html"):
    vote = get_object_or_404(Vote, pk=vid)
    user = request.user 
    if vote.user != user and vote.question.user != user: # 必须在这两个人之间才有意义
        return HttpResponse("error")
    if request.method == "POST":
        form = ConversationForm(request, data=request.POST)
        if form.is_valid():
            form.save(request.user, vote)
    else:
        form = ConversationForm()
    conversations = Conversation.objects.filter(vote=vote)
    return render_to_response(template_name, 
                            {"conversations": conversations,
                            "vote":vote, "form":form
                            },
                              context_instance=RequestContext(request))
    
    
    
    

